import {Component} from "@angular/core";
import {AlertController, NavController, NavParams} from "ionic-angular";
import {HttpService} from "../../../providers/http.service";
import {CommonFunc} from "../../../providers/commonFunc";
import {Storage} from "@ionic/storage";
import {ScanPage} from "../../scan/scan";
import {CheckItem} from "../check-item/check-item";
import {ElevatorCheckModify} from "../elevator-check-modify/elevator-check-modify";

@Component({
  templateUrl: "elevator-check.html"
})
export class ElevatorCheck {
    elevatorResult: any = [];
    elevatorTotalResult: any = [];
    itemResult: any = [];
    elevatorModifyResult: any = [];
    checkedNum: number = 0;

    constructor(
        private navParams: NavParams,
        private commonFunc: CommonFunc,
        private httpService: HttpService,
        private storage: Storage,
        private navController: NavController,
        public alertCtrl: AlertController
    ) {
        //获取电梯检查后修改的结果
        let modifyResult = this.navParams.get("result");
        //存储每次检查结果
        this.elevatorResult = this.navParams.get("checkRes");
        this.storage.get("elevatorTotalResult").then(data => {
            if (data != null && data.length > 0) {
                this.checkedNum = data.length;
                for (let i = 0; i < data.length; i++) {
                    if (data[i] != null) {
                        if(this.elevatorResult != null && typeof(this.elevatorResult) != 'undefined'
                            && data[i][0].deviceNum == this.elevatorResult[0].deviceNum){
                            continue;
                        }
                        this.elevatorTotalResult.push(data[i]);
                    }
                }
            }
            if (this.elevatorResult != null && this.elevatorResult != "") {
                this.elevatorTotalResult.push(this.elevatorResult);
                this.storage.set("elevatorTotalResult", this.elevatorTotalResult);
            } else {
                this.storage.set("elevatorTotalResult", this.elevatorTotalResult);
            }
            //如果修改了某台电梯检测结果，则更新到电梯检查结果中
            if (modifyResult != null && modifyResult != undefined) {
                for (let i = 0; i < this.elevatorTotalResult.length; i++) {
                    if (this.elevatorTotalResult[i][0].deviceNum == modifyResult[0].deviceNum) {
                        this.elevatorTotalResult[i] = modifyResult;
                    }
                }
            }
            //this.storage.remove("elevatorResult");
            //this.storage.remove("elevatorTotalResult");
        });
    }

    //
    scanner() {
        this.navController.push(ScanPage, {
            id: this.navParams.get("id"),
            pdId: this.navParams.get("pdId"),
            beCkedPartyName: this.navParams.get("beCkedPartyName"),
            beCkedPartyId: this.navParams.get("beCkedPartyId"),
            pdType: this.navParams.get("pdType"),
            type: "3",
            workType: "check",
            state: this.navParams.get("state"),
        })
    }

    //
    submit() {
        if (this.elevatorTotalResult != null && this.elevatorTotalResult.length > 0) {
            for (let i = 0; i < this.elevatorTotalResult.length; i++) {
                for (let j = 0; j < this.elevatorTotalResult[i].length; j++) {
                    this.itemResult.push(this.elevatorTotalResult[i][j]);
                }
            }
        }
        this.storage.set("elevatorResult",this.itemResult);
        this.navController.push(CheckItem, {
            checkRes: this.itemResult,
            type: "3", /*this.navParams.get("type")*/
            id: this.navParams.get("id"),
            pdId: this.navParams.get("pdId"),
            beCkedPartyName: this.navParams.get("beCkedPartyName"),
            beCkedPartyId: this.navParams.get("beCkedPartyId"),
            pdType: this.navParams.get("pdType"),
            state: this.navParams.get("state"),
            checkedNum: this.checkedNum,
            mark1:"mark1",
            mark2:this.navParams.get("mark2")
        });
    }

    modify(p) {
        //选择需要修改的电梯
        if (this.elevatorTotalResult != null && this.elevatorTotalResult.length > 0) {
            for (let i = 0; i < this.elevatorTotalResult.length; i++) {
                if (p == i) {
                    this.elevatorModifyResult = this.elevatorTotalResult[i];
                }
            }
        }
        this.navController.push(ElevatorCheckModify, {
            elevatorModifyResult: this.elevatorModifyResult,
            type: "3",
            id: this.navParams.get("id"),
            pdId: this.navParams.get("pdId"),
            beCkedPartyName: this.navParams.get("beCkedPartyName"),
            beCkedPartyId: this.navParams.get("beCkedPartyId"),
            pdType: this.navParams.get("pdType"),
            state: this.navParams.get("state"),
        });
    }

}
